// http://chst.vip:1234/api/getcouponproduct?couponid=0
let ul = document.querySelector('#ul')
function getUrlParams() {
    let url = location.href;
    let regExp = /.*\?([^\?]+)/
    let query = {};//最终处理好的参数
    let search = regExp.exec(url);
    //productid=23&NAME=123"
    // console.log(search);
    search = search[1];
    //有没有多个query参数,有的话肯定以&进行连接
    let searchArr = [];
    if (search.indexOf("&")) {
        searchArr = search.split("&")
    } else {
        search = search.split()
    }

    searchArr.forEach(item => {
        let queryArr = item.split("=");
        query[queryArr[0]] = queryArr[1]
    })

    return query
}

let query = getUrlParams();

fetch("http://chst.vip:1234/api/getcouponproduct?couponid=" + query.couponid)
    .then(body => body.json())
    .then(res => {
        console.log(res);

        let html = ""
        res.result.forEach(item => {
            html += ` <li couponProductId=${item.couponProductId}>
        <div> ${item.couponProductImg}
        <p>
        <span>${item.couponProductName}</span>
        <span>${item.couponProductPrice}</span>
        </p>
        </div>
        <p>${item.couponProductTime}</p>
        </li>`
        })
        ul.innerHTML = html

        let lis = [...ul.children]
        let html2 = ""
        lis.forEach(li => {
            li.onclick = function () {
                $("#box")[0].style.display = 'block'
                $("#box").stop().animate({ top: 0 }, 500)
                res.result.forEach(item => {
                    html2 += ` <li>${item.couponProductImg}
                </li>`
                })
                $(".gun")[0].innerHTML = html2
                let index = this.getAttribute("couponproductid")
                $(".gun").animate({left:-index*$('.gun>li').width()},0)
                $(".right")[0].onclick =  ()=> {
                    console.log( res.result.length);
                    if(index< res.result.length-1){
                        index++
                    $(".gun").stop().animate({left:-index*$(".gun>li").width()},500)
                    }else{
                        alert("最后一页了")
                    }
                    
                }
                $(".left")[0].onclick =  ()=> {
                    if(index>0){
                        index--
                        $(".gun").stop().animate({left:-index*$(".gun>li").width()},500)
                    }else{
                        alert("已经是第一页了")
                    }
                   
                }
                $("#X")[0].onclick = function () {
                    $("#box").stop().animate({ top: "-120%" }, 500)
                }
            }
        })


    })



